#include "database.h"
#include <string.h>

Database::Database()
{
    mysqlpp::Connection con(false);
}

bool Database::connect(std::string db, std::string server, std::string user, std::string pass)
{
    if (!con.connect(db.c_str(), server.c_str(), user.c_str(), pass.c_str()))
        return false;

    //output.write("Connected to MySQL server on %s", server.c_str());

    return true;
}

bool Database::account_create(std::string name, std::string pass)
{
    mysqlpp::Query query = con.query();
    query << "SELECT * FROM account WHERE username = '" << name << "'";
    mysqlpp::StoreQueryResult res = query.store();

    if (res.empty())
    {
        query << "INSERT INTO account (username, pass_hash) VALUES ('" << name << "', '" << pass << "')";
        if (query.exec())
            return true;
    }

    return false;
}

bool Database::account_login(std::string name, std::string pass)
{
    mysqlpp::Query query = con.query();
    query << "SELECT * FROM account WHERE username = '" << name << "' AND pass_hash = '" << pass << "'";
    mysqlpp::StoreQueryResult res = query.store();

    if (res.empty())
    {
        return false;
    }

    return true;
}

bool Database::character_create(int owner, std::string name)
{
    mysqlpp::Query query = con.query();
    query << "SELECT * FROM characters WHERE name = '" << name << "'";
    mysqlpp::StoreQueryResult res = query.store();

    if (res.empty())
    {
        query << "INSERT INTO characters (owner, name) VALUES (" << owner << ", '" << name << "')";
        if (query.exec())
            return true;
    }

    return false;
}

account Database::get_account(int id)
{
    account acc;

    mysqlpp::Query query = con.query();
    query << "SELECT * FROM account WHERE id = " << id;

    if (mysqlpp::StoreQueryResult res = query.store()) {
        acc.id = id;
        res[0]["username"].to_string(acc.username);
        res[0]["pass_hash"].to_string(acc.pass_hash);
        acc.admin = res[0]["admin"];

        return acc;
    }
}

character Database::get_character(int id)
{
    character c;

    mysqlpp::Query query = con.query();
    query << "SELECT * FROM characters WHERE id = " << id;

    if (mysqlpp::StoreQueryResult res = query.store()) {
        c.id = id;
        c.owner = res[0]["owner"];
        res[0]["name"].to_string(c.name);
        c.type = res[0]["type"];
        c.level = res[0]["level"];
        c.xp = res[0]["xp"];
        c.money = res[0]["money"];
        c.pos_x = res[0]["pos_x"];
        c.pos_y = res[0]["pos_y"];
        c.map = res[0]["map"];

        return c;
    }
}

std::vector <character> Database::get_characters(int id)
{
    std::vector<character> characters;

    mysqlpp::Query query = con.query();
    query << "SELECT * FROM characters WHERE owner = " << id;

    if (mysqlpp::StoreQueryResult res = query.store()) {
        for (size_t i = 0; i < res.num_rows(); ++i) {
            character c;

            c.id = res[i]["id"];
            c.owner = res[i]["owner"];
            res[i]["name"].to_string(c.name);
            c.type = res[i]["type"];
            c.level = res[i]["level"];
            c.xp = res[i]["xp"];
            c.money = res[i]["money"];
            c.pos_x = res[i]["pos_x"];
            c.pos_y = res[i]["pos_y"];
            c.map = res[i]["map"];

            characters.push_back(c);
        }
    }

    return characters;
}

int Database::accountNameToID(std::string name)
{
    mysqlpp::Query query = con.query();
    query << "SELECT id FROM account WHERE username = '" << name << "'";

    if (mysqlpp::StoreQueryResult res = query.store()) {
        return res[0][0];
    }

    return -1;
}
